package dbinit;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import core.attribute.model.Attribute;
import core.attribute.model.AttributeSet;
import core.attribute.model.FlatAttributes;
import core.util.HibernateCoreUtil;

public class InitFlatAttributes 
{
	public static void refresh()
	{
	
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				session.createQuery("delete from FlatAttributes");
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}

	}
	public static Long doInit(AttributeSet as, Attribute a)
	{
		Long uuid = null ;
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		FlatAttributes fa = new FlatAttributes();
		try
		{
			transaction = session.beginTransaction();
			fa.setAttribute(a);
			fa.setAttributeSet(as);
			uuid = (Long) session.save(fa);
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return uuid ;
	}
}
